package com.whm.hrmanagement.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whm.common.core.domain.model.LoginUser;
import com.whm.common.utils.DateUtils;
import com.whm.hrmanagement.domain.OaQpglqpms;
import com.whm.hrmanagement.domain.OaTargetTracking;
import com.whm.hrmanagement.domain.dto.Extra;
import com.whm.system.domain.SysFileInfo;
import com.whm.system.mapper.SysFileInfoMapper;
import com.whm.system.service.ISysFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.whm.hrmanagement.mapper.OaTrainingInfoMapper;
import com.whm.hrmanagement.domain.OaTrainingInfo;
import com.whm.hrmanagement.service.IOaTrainingInfoService;

import static com.whm.common.utils.PageUtils.startPage;

/**
 * 培训信息记录Service业务层处理
 * 
 * @author whm
 * @date 2024-02-19
 */
@Service
public class OaTrainingInfoServiceImpl implements IOaTrainingInfoService 
{
    @Autowired
    private OaTrainingInfoMapper oaTrainingInfoMapper;
    @Autowired
    private SysFileInfoMapper sysFileInfoMapper;
    @Autowired
    private ISysFileInfoService iSysFileInfoService;
    /**
    /**
     * 查询培训信息记录
     * 
     * @param id 培训信息记录主键
     * @return 培训信息记录
     */
    @Override
    public OaTrainingInfo selectOaTrainingInfoById(Long id)
    {
        return oaTrainingInfoMapper.selectOaTrainingInfoById(id);
    }

    /**
     * 查询培训信息记录列表
     * 
     * @param oaTrainingInfo 培训信息记录
     * @return 培训信息记录
     */
    @Override
    public List<OaTrainingInfo> selectOaTrainingInfoList(OaTrainingInfo oaTrainingInfo)
    {
        LoginUser user = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user.getDeptId() != 204 && user.getDeptId() != 200 &&  user.getUserId() != 109 && user.getUserId() != 1) {
            List<OaTrainingInfo> oaTrainingInfos = oaTrainingInfoMapper.selectOaTrainingInfoList(oaTrainingInfo);

            return oaTrainingInfos.stream().filter(e -> e.getDepartmentName() == user.getDeptId()).collect(Collectors.toList());
        }
        startPage();
        return oaTrainingInfoMapper.selectOaTrainingInfoList(oaTrainingInfo);
    }

    /**
     * 新增培训信息记录
     * 
     * @param oaTrainingInfo 培训信息记录
     * @return 结果
     */
    @Override
    public int insertOaTrainingInfo(JSONObject data)
    {
        String jsonString = JSONObject.toJSONString(data.getJSONObject("form"));
        OaTrainingInfo oaTrainingInfo = JSONObject.parseObject(jsonString, OaTrainingInfo.class);
        String jsonString2 = JSONArray.toJSONString(data.getJSONArray("Extra"));
        List<Extra> extras = JSONArray.parseArray(jsonString2,  Extra.class);
        oaTrainingInfo.setCreateTime(DateUtils.getNowDate());
        Integer result = oaTrainingInfoMapper.insertOaTrainingInfo(oaTrainingInfo);
        QueryWrapper<SysFileInfo> qw = new QueryWrapper<>();
        for (Extra obj : extras) {
            System.out.println(obj.getFilePath());
            qw.lambda().eq(SysFileInfo::getFilePath,obj.getFilePath());
            SysFileInfo  sysFileInfo =  sysFileInfoMapper.selectOne(qw);
            sysFileInfo.setReltype("OaTrainingInfo");
            sysFileInfo.setRelId(oaTrainingInfo.getId());
            iSysFileInfoService.updateSysFileInfo(sysFileInfo);
        }
        return result;
    }

    /**
     * 修改培训信息记录
     * 
     * @param oaTrainingInfo 培训信息记录
     * @return 结果
     */
    @Override
    public int updateOaTrainingInfo(JSONObject data)
    {
        String jsonString = JSONObject.toJSONString(data.getJSONObject("form"));
        OaTrainingInfo oaTrainingInfo = JSONObject.parseObject(jsonString, OaTrainingInfo.class);
        String jsonString2 = JSONArray.toJSONString(data.getJSONArray("Extra"));
        List<Extra> extras = JSONArray.parseArray(jsonString2,  Extra.class);
        QueryWrapper<SysFileInfo> qw = new QueryWrapper<>();
        qw.lambda().eq(SysFileInfo::getRelId,oaTrainingInfo.getId());
        List<SysFileInfo>  sysFileInfoList =  sysFileInfoMapper.selectList(qw);
        for (SysFileInfo obj : sysFileInfoList) {
            obj.setRelId(0L);
            iSysFileInfoService.updateSysFileInfo(obj);
        }
        for (Extra obj2 : extras) {
            QueryWrapper<SysFileInfo> qw2 = new QueryWrapper<>();
            qw2.lambda().eq(SysFileInfo::getFilePath,obj2.getFilePath());
            SysFileInfo  sysFileInfo2 =  sysFileInfoMapper.selectOne(qw2);;
            sysFileInfo2.setReltype("OaTrainingInfo");
            sysFileInfo2.setRelId(oaTrainingInfo.getId());
            iSysFileInfoService.updateSysFileInfo(sysFileInfo2);
        }
        oaTrainingInfo.setUpdateTime(DateUtils.getNowDate());
        return oaTrainingInfoMapper.updateOaTrainingInfo(oaTrainingInfo);
    }

    /**
     * 批量删除培训信息记录
     * 
     * @param ids 需要删除的培训信息记录主键
     * @return 结果
     */
    @Override
    public int deleteOaTrainingInfoByIds(Long[] ids)
    {
        return oaTrainingInfoMapper.deleteOaTrainingInfoByIds(ids);
    }

    /**
     * 删除培训信息记录信息
     * 
     * @param id 培训信息记录主键
     * @return 结果
     */
    @Override
    public int deleteOaTrainingInfoById(Long id)
    {
        return oaTrainingInfoMapper.deleteOaTrainingInfoById(id);
    }
}
